﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using Sgml;

namespace CustomsNuke.Scheduling.Utility
{
    public class HtmlParseUtils
    {
        public static string FormatHtml(string html, bool upper, bool formatted)
        {
            try
            {
                html = html.Replace("]]>", "");
                SgmlReader r = new SgmlReader();
                r.DocType = "HTML";
                r.InputStream = new StringReader(html);
                if (upper)
                {
                    r.CaseFolding = CaseFolding.ToUpper;
                }
                StringWriter sw = new StringWriter();
                XmlTextWriter w = new XmlTextWriter(sw);
                if (formatted)
                {
                    w.Formatting = Formatting.None;
                    r.WhitespaceHandling = WhitespaceHandling.None;
                }
                else
                {
                    w.Formatting = Formatting.Indented;
                    r.WhitespaceHandling = WhitespaceHandling.All;
                }
                while (!r.EOF)
                {
                    w.WriteNode(r, true);
                }
                w.Close();
                return sw.ToString();
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        public static string GetSubString(string text, string startPattern, string mediumPattern, string endPattern, string replacement1, string replacement2, string replacement3)
        {
            RegexOptions options = RegexOptions.Singleline;
            Regex regex;
            bool isMatch;
            Match match;
            string pattern;
            string returnText;

            if (string.IsNullOrEmpty(mediumPattern))
            {
                pattern = startPattern + ".*?" + endPattern;
            }
            else
            {
                pattern = startPattern + "(" + mediumPattern + ")*.*?" + endPattern;
            }

            regex = new Regex(pattern, options);
            isMatch = regex.IsMatch(text);
            if (isMatch)
            {
                match = regex.Match(text);
                returnText = Regex.Replace(match.Value, startPattern, "");
                returnText = Regex.Replace(returnText, endPattern.Replace("?", ""), "");
                if (!string.IsNullOrEmpty(replacement1))
                {
                    returnText = returnText.Replace(replacement1, "");
                }
                if (!string.IsNullOrEmpty(replacement2))
                {
                    returnText = returnText.Replace(replacement2, "");
                }
                if (!string.IsNullOrEmpty(replacement3))
                {
                    returnText = returnText.Replace(replacement3, "");
                }
            }
            else
            {
                returnText = "";
            }
            return returnText;
        }

        public static List<string> GetSubStrings(string text, string startPattern, string mediumPattern, string endPattern, string replacement1, string replacement2, string replacement3)
        {
            RegexOptions options = RegexOptions.Singleline;
            Regex regex;
            bool isMatch;
            MatchCollection matches;
            string pattern;
            string returnText;
            List<string> returnTextList = new List<string>();

            if (string.IsNullOrEmpty(mediumPattern))
            {
                pattern = startPattern + ".*?" + endPattern;
            }
            else
            {
                pattern = startPattern + "(" + mediumPattern + ")*.*?" + endPattern;
            }

            regex = new Regex(pattern, options);
            isMatch = regex.IsMatch(text);
            if (isMatch)
            {
                matches = regex.Matches(text);
                foreach (Match match in matches)
                {
                    returnText = match.Value;
                    if (!string.IsNullOrEmpty(replacement1))
                    {
                        returnText = returnText.Replace(replacement1, "");
                    }
                    if (!string.IsNullOrEmpty(replacement2))
                    {
                        returnText = returnText.Replace(replacement2, "");
                    }
                    if (!string.IsNullOrEmpty(replacement3))
                    {
                        returnText = returnText.Replace(replacement3, "");
                    }
                    returnTextList.Add(returnText);
                }
                return returnTextList;
            }
            else
            {
                return null;
            }
        }

        public static List<string> GetSubStrings(string text, string pattern, string replacePattern)
        {
            RegexOptions options = RegexOptions.Singleline;
            Regex regex;
            bool isMatch;
            MatchCollection matches;
            string returnText;
            List<string> returnTextList = new List<string>();

            regex = new Regex(pattern, options);
            isMatch = regex.IsMatch(text);
            if (isMatch)
            {
                matches = regex.Matches(text);
                foreach (Match match in matches)
                {
                    returnText = match.Value;

                    if (!string.IsNullOrEmpty(replacePattern))
                    {
                        returnText = Regex.Replace(returnText, replacePattern, "", options);
                    }
                    
                    returnTextList.Add(returnText);
                }
                return returnTextList;
            }
            else
            {
                return null;
            }
        }

        public static string ParsePage(string url, Encoding encoding = null)
        {
            try
            {
                

                var web = new WebClient();
                if (encoding == null)
                {
                    web.Encoding = Encoding.UTF8;
                }
                else
                {
                    web.Encoding = encoding;
                }

                //WebProxy proxy = new WebProxy("http://jpnproxy.fareast.corp.microsoft.com:80/", true);
                //web.Proxy = proxy;
                string content = HtmlParseUtils.FormatHtml(web.DownloadString(url), false, true);
                return content;
            }
            catch (Exception ex)
            {
                ex.HelpLink = url;
                return null;
            }
        }

        public static string ParsePage(string url, byte[] postData, WebHeaderCollection headers)
        {
            try
            {
                var web = new WebClient();
                web.Headers.Add("ContentLength", postData.Length.ToString());

                if (headers != null & headers.Count > 0)
                {
                    web.Headers.Add(headers);
                }

                var r = Encoding.UTF8.GetString(web.UploadData(url, "POST", postData));
                var content = HtmlParseUtils.FormatHtml(r, false, true);

                return content;
            }
            catch (Exception ex)
            {
                ex.HelpLink = url;
                return string.Empty;
            }
        }
    }
}
